<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .login-container {
            background-color: white;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 350px;
        }

        .login-title {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
            font-size: 24px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #555;
            font-weight: bold;
        }

        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            box-sizing: border-box;
        }

        .form-group input:focus {
            outline: none;
            border-color: #4CAF50;
        }

        .login-btn {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            margin-bottom: 15px;
        }

        .login-btn:hover {
            background-color: #45a049;
        }

        .forgot-password {
            text-align: center;
            color: #666;
            font-size: 14px;
        }

        .forgot-password a {
            color: #4CAF50;
            text-decoration: none;
        }

        .forgot-password a:hover {
            text-decoration: underline;
        }

        .register-link {
            text-align: center;
            margin-top: 20px;
            color: #666;
            font-size: 14px;
        }

        .register-link a {
            color: #4CAF50;
            text-decoration: none;
        }

        .register-link a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body>
<div class="login-container">
    <div class="logo">
        <img src="vegetable-logo.png" alt="蔬菜商城Logo">
        <h2>蔬菜商城</h2>
    </div>
    <form action="sys/user/login" th:action="@{sys/user/login}" th:method="post">
        <div class="form-group">
            <label for="username">用户名：</label>
            <input type="text" id="username" name="username" placeholder="请输入用户名" required>
        </div>
        <div class="form-group">
            <label for="password">密码：</label>
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
        </div>
<!--        <div class="form-group">-->
<!--            <label>-->
<!--                记住我<input type="checkbox" name="remember" id="remember">-->
<!--            </label>-->
<!--        </div>-->
        <button type="submit" class="login-btn">登录</button>
    </form>
    <div class="forgot-password">
        <a href="#">忘记密码？</a>
    </div>
    <div class="register-link">
        还没有账号？<a href="/register">立即注册</a> <a href="/">返回首页</a>
    </div>
</div>
</body>

</html>
<script>
    // 获取表单元素
    const loginForm = document.querySelector('form');
    const usernameInput = document.getElementById('username');
    const passwordInput = document.getElementById('password');
    // const rememberCheckbox = document.getElementById('remember');

    // 监听表单提交事件
    loginForm.addEventListener('submit', async function(e) {
        e.preventDefault(); // 阻止表单默认提交行为

        // 获取用户输入的数据
        const loginData = {
            username: usernameInput.value,
            password: passwordInput.value,
            // remember: rememberCheckbox.checked
        };

        try {
            // 调用后端登录接口
            const response = await fetch('sys/user/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(loginData)
            });
            // 检查响应状态
            if (response.ok) {
                const result = await response.json();
                console.log('响应数据：',result)
                // 获取后端返回的字段
                const code = result.code;
                const message = result.message;
                const data = result.data;

                console.log('响应 code:', code);
                console.log('响应 message:', message);
                console.log('响应 data:', data);
                // 登录成功处理
                if (result.code===0) {
                    // alert('登录成功！');
                    // 可以跳转到其他页面
                     window.location.href = '/info';
                } else {
                    alert('登录失败：' + result.message);
                }
            } else {
                alert('网络错误，请稍后重试');
            }
        } catch (error) {
            console.error('登录请求失败：', error);
            alert('登录失败，请检查网络连接');
        }
    });

    // 可选：添加输入验证
    function validateForm() {
        if (!usernameInput.value.trim()) {
            alert('请输入用户名');
            return false;
        }
        if (!passwordInput.value.trim()) {
            alert('请输入密码');
            return false;
        }
        return true;
    }

    // 在提交前验证表单
    loginForm.addEventListener('submit', function(e) {
        if (!validateForm()) {
            e.preventDefault();
        }
    });
</script>
